package ru.arriah.servicedesk.web.admin.action;

import java.io.IOException;
import java.util.Collection;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.DepartmentBean;
import ru.arriah.servicedesk.ejb.OrganizationManagerLocalHome;
import ru.arriah.servicedesk.ejb.OrganizationManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.InternalException;

public class SetDepStatusAction extends DumbAction {
   protected OrganizationManagerLocalHome organizationManagerHome;

   public SetDepStatusAction() throws NamingException {
      super();

      organizationManagerHome =
         Utils.getOrganizationManagerHomeInterface();
   }

   
   public String execute(
      ActionMapping actionMapping,
      HttpServletRequest request,
      HttpServletResponse response
   ) throws IOException, ServletException {
     try { 
              
       int departmentId = idVal(request.getParameter("department_id"));
       String status = request.getParameter("status");               
       
       OrganizationManagerLocalObject organizationManager = 
         organizationManagerHome.create();
       
       if (status.equals("delete")) {
         organizationManager.deleteDepartment(departmentId);
         
         addInfMessage(         
            request,
            config.getProperty("INF.DEPARTMENT.DELETED")
         ); 
         
       } else if (status.equals("restore")) {
         organizationManager.restoreDepartment(departmentId);
         
         addInfMessage(
            request,
            config.getProperty("INF.DEPARTMENT.RESTORED")
         );
         
       }               
              
//Prepare organization List
       
       int organizationId = 
         idVal(request.getParameter("organization_id"));
       
       String order = request.getParameter("order");
       String orderBy = request.getParameter("orderBy");
       String type = request.getParameter("type");
                   
       order = (order == null) ? "" : order;                
       orderBy = (orderBy == null) ? "" : orderBy;
       type = (type == null) ? "" : type;
       
       if (type.equals("all")) {
         status = "'active', 'inactive'";
       } else if(type.equals("active")) {
         status = "'active'";
       } else if(type.equals("inactive")) {
         status = "'inactive'";
       } else 
         status = "'active'";       
       
       Collection<DepartmentBean> departmentsList = 
         organizationManager.selectDepartments(
           organizationId,
           orderBy, 
           order,
           status
         );
       
       organizationManager.remove();       

       request.setAttribute("order", (order.equals("ASC") ? "DESC" : "ASC"));
       request.setAttribute("type", type);
       request.setAttribute("departmentsList", departmentsList);
       request.setAttribute("organizationId", organizationId);
       
       return actionMapping.getSuccessTarget();
    } catch (Exception e) {
       handleException(e, "execute");
       throw new InternalException("ErrorMessage.internal");
    }
   }
}